package com.jerei.plugin.dao.impl;

import com.jerei.plugin.dao.IWcmCommonWebsiteDao;
import com.jerei.plugin.entity.WcmCommonWebsite;
import com.jerei.plugin.utils.JrJdbcTemplate;
import com.jerei.utils.condition.ConditionEntity;
import com.jerei.utils.condition.ConditionTools;
import com.jerei.utils.condition.SQLEntity;
import com.jerei.utils.spring.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by sunfeihu on 2016/10/20.
 */
@Repository
public class WcmCommonWebsiteDaoImpl implements IWcmCommonWebsiteDao {

    @Autowired
    private JrJdbcTemplate jrJdbcTemplate;

    @Override
    public WcmCommonWebsite save(WcmCommonWebsite entity) {
        final String sql = " insert into wcm_common_website (uuid,add_date,add_ip,add_user,update_date,update_ip,update_user,name,pid,path,gid,site_type,domain,weixin_appid,weixin_secret,weixin_accounts,weixin_token,sms_config_id,website_config_id,no,customer_service_telephone) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        KeyHolder keyHolder = new GeneratedKeyHolder();
        final WcmCommonWebsite finalEntity = entity;
        this.jrJdbcTemplate.getJdbcTemplate().update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement ps = connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
                ps.setObject(1, finalEntity.getUuid());
                if (null != finalEntity.getAddDate()) {
                    ps.setObject(2, new java.sql.Timestamp(finalEntity.getAddDate().getTime()));
                } else {
                    ps.setObject(2, null);
                }
                ps.setObject(3, finalEntity.getAddIp());
                ps.setObject(4, finalEntity.getAddUser());
                if (null != finalEntity.getUpdateDate()) {
                    ps.setObject(5, new java.sql.Timestamp(finalEntity.getUpdateDate().getTime()));
                } else {
                    ps.setObject(5, null);
                }
                ps.setObject(6, finalEntity.getUpdateIp());
                ps.setObject(7, finalEntity.getUpdateUser());
                ps.setObject(8, finalEntity.getName());
                ps.setObject(9, finalEntity.getPid());
                ps.setObject(10, finalEntity.getPath());
                ps.setObject(11, finalEntity.getGid());
                ps.setObject(12, finalEntity.getSiteType());
                ps.setObject(13, finalEntity.getDomain());
                ps.setObject(14, finalEntity.getWeixinAppid());
                ps.setObject(15, finalEntity.getWeixinSecret());
                ps.setObject(16, finalEntity.getWeixinAccounts());
                ps.setObject(17, finalEntity.getWeixinToken());
                ps.setObject(18, finalEntity.getSmsConfigId());
                ps.setObject(19, finalEntity.getWebsiteConfigId());
                ps.setObject(20, finalEntity.getNo());
                ps.setObject(21, finalEntity.getCustomerServiceTelephone());
                return ps;
            }
        }, keyHolder);
        Long id = keyHolder.getKey().longValue();
        return this.find(id);
    }

    @Override
    public int delete(Long id) {
        final String sql = " delete from wcm_common_website where id = ? ";
        return this.jrJdbcTemplate.getJdbcTemplate().update(sql, new Object[]{
                id
        });
    }

    @Override
    public int update(WcmCommonWebsite entity) {
        final String sql = " update wcm_common_website set uuid=?,add_date=?,add_ip=?,add_user=?,update_date=?,update_ip=?,update_user=?,name=?,pid=?,path=?,gid=?,site_type=?,domain=?,weixin_appid=?,weixin_secret=?,weixin_accounts=?,weixin_token=?,sms_config_id=?,website_config_id=?,no=?,customer_service_telephone=? where id = ?";
        return this.jrJdbcTemplate.getJdbcTemplate().update(sql, new Object[]{
                entity.getUuid(),
                entity.getAddDate(),
                entity.getAddIp(),
                entity.getAddUser(),
                entity.getUpdateDate(),
                entity.getUpdateIp(),
                entity.getUpdateUser(),
                entity.getName(),
                entity.getPid(),
                entity.getPath(),
                entity.getGid(),
                entity.getSiteType(),
                entity.getDomain(),
                entity.getWeixinAppid(),
                entity.getWeixinSecret(),
                entity.getWeixinAccounts(),
                entity.getWeixinToken(),
                entity.getSmsConfigId(),
                entity.getWebsiteConfigId(),
                entity.getNo(),
                entity.getCustomerServiceTelephone(),
                entity.getId()
        });
    }

    @Override
    public WcmCommonWebsite find(Long id) {
        final String sql = " select * from wcm_common_website where id = ? ";
        return this.jrJdbcTemplate.getJdbcTemplate().queryForObject(sql, new Object[]{id}, new WcmCommonWebsiteRowerMapper());
    }

    @Override
    public void save(List<WcmCommonWebsite> list) {
        if (null != list && list.size() > 0) {
            final String sql = " insert into wcm_common_website (uuid,add_date,add_ip,add_user,update_date,update_ip,update_user,name,pid,path,gid,site_type,domain,weixin_appid,weixin_secret,weixin_accounts,weixin_token,sms_config_id,website_config_id,no,customer_service_telephone) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            List<Object[]> params = new ArrayList<Object[]>();
            Object[] objArr = null;
            WcmCommonWebsite entity = null;
            for (int i = 0; i < list.size(); i++) {
                entity = list.get(i);
                objArr = new Object[21];
                objArr[0] = entity.getUuid();
                objArr[1] = entity.getAddDate();
                objArr[2] = entity.getAddIp();
                objArr[3] = entity.getAddUser();
                objArr[4] = entity.getUpdateDate();
                objArr[5] = entity.getUpdateIp();
                objArr[6] = entity.getUpdateUser();
                objArr[7] = entity.getName();
                objArr[8] = entity.getPid();
                objArr[9] = entity.getPath();
                objArr[10] = entity.getGid();
                objArr[11] = entity.getSiteType();
                objArr[12] = entity.getDomain();
                objArr[13] = entity.getWeixinAppid();
                objArr[14] = entity.getWeixinSecret();
                objArr[15] = entity.getWeixinAccounts();
                objArr[16] = entity.getWeixinToken();
                objArr[17] = entity.getSmsConfigId();
                objArr[18] = entity.getWebsiteConfigId();
                objArr[19] = entity.getNo();
                objArr[20] = entity.getCustomerServiceTelephone();
                params.add(objArr);
            }
            this.jrJdbcTemplate.getJdbcTemplate().batchUpdate(sql, params);
        }
    }

    @Override
    public void delete(List<Long> ids) {
        if (null != ids && ids.size() > 0) {
            final String sql = " delete from wcm_common_website where id = ? ";
            List<Object[]> params = new ArrayList<Object[]>();
            Object[] objArr = null;
            for (int i = 0; i < ids.size(); i++) {
                objArr = new Object[1];
                objArr[0] = ids.get(i);
                params.add(objArr);
            }
            this.jrJdbcTemplate.getJdbcTemplate().batchUpdate(sql, params);
        }
    }

    @Override
    public void update(List<WcmCommonWebsite> list) {
        if (null != list && list.size() > 0) {
            final String sql = " update wcm_common_website set uuid=?,add_date=?,add_ip=?,add_user=?,update_date=?,update_ip=?,update_user=?,name=?,pid=?,path=?,gid=?,site_type=?,domain=?,weixin_appid=?,weixin_secret=?,weixin_accounts=?,weixin_token=?,sms_config_id=?,website_config_id=?,no=?,customer_service_telephone=? where id = ?";
            List<Object[]> params = new ArrayList<Object[]>();
            Object[] objArr = null;
            WcmCommonWebsite entity = null;
            for (int i = 0; i < list.size(); i++) {
                entity = list.get(i);
                objArr = new Object[21];
                objArr[0] = entity.getUuid();
                objArr[1] = entity.getAddDate();
                objArr[2] = entity.getAddIp();
                objArr[3] = entity.getAddUser();
                objArr[4] = entity.getUpdateDate();
                objArr[5] = entity.getUpdateIp();
                objArr[6] = entity.getUpdateUser();
                objArr[7] = entity.getName();
                objArr[8] = entity.getPid();
                objArr[9] = entity.getPath();
                objArr[10] = entity.getGid();
                objArr[11] = entity.getSiteType();
                objArr[12] = entity.getDomain();
                objArr[13] = entity.getWeixinAppid();
                objArr[14] = entity.getWeixinSecret();
                objArr[15] = entity.getWeixinAccounts();
                objArr[16] = entity.getWeixinToken();
                objArr[17] = entity.getSmsConfigId();
                objArr[18] = entity.getWebsiteConfigId();
                objArr[19] = entity.getNo();
                objArr[20] = entity.getCustomerServiceTelephone();
                objArr[21] = entity.getId();
                params.add(objArr);
            }
            this.jrJdbcTemplate.getJdbcTemplate().batchUpdate(sql, params);
        }
    }

    @Override
    public PageBean<WcmCommonWebsite> page(int nowPage, int pageSize, ConditionEntity conditionEntity) {
        SQLEntity sqlEntity = ConditionTools.conditionEntities2SQL(conditionEntity);
        final String sql = " select * from wcm_common_website where 1 = 1 " + sqlEntity.getSql() + " order by " + sqlEntity.getOrder();
        return new PageBean<WcmCommonWebsite>(sql, sqlEntity.getParams(), nowPage, pageSize, new WcmCommonWebsiteRowerMapper(), this.jrJdbcTemplate.getJdbcTemplate());
    }
}


class WcmCommonWebsiteRowerMapper implements RowMapper<WcmCommonWebsite> {
    @Override
    public WcmCommonWebsite mapRow(ResultSet rs, int rowNum) throws SQLException {
        ResultSetMetaData rsmd = rs.getMetaData();
        int columnCount = rsmd.getColumnCount();
        Set<String> fieldSet = new HashSet<String>();
        for (int i = 1; i <= columnCount; i++) {
            fieldSet.add(rsmd.getColumnName(i));
        }
        WcmCommonWebsite entity = new WcmCommonWebsite();
        if (fieldSet.contains("id") && null != rs.getObject("id")) {
            entity.setId(rs.getLong("id"));
        }
        if (fieldSet.contains("uuid") && null != rs.getObject("uuid")) {
            entity.setUuid(rs.getString("uuid"));
        }
        if (fieldSet.contains("add_date") && null != rs.getObject("add_date")) {
            entity.setAddDate(new java.util.Date(rs.getTimestamp("add_date").getTime()));
        }
        if (fieldSet.contains("add_ip") && null != rs.getObject("add_ip")) {
            entity.setAddIp(rs.getString("add_ip"));
        }
        if (fieldSet.contains("add_user") && null != rs.getObject("add_user")) {
            entity.setAddUser(rs.getString("add_user"));
        }
        if (fieldSet.contains("update_date") && null != rs.getObject("update_date")) {
            entity.setUpdateDate(new java.util.Date(rs.getTimestamp("update_date").getTime()));
        }
        if (fieldSet.contains("update_ip") && null != rs.getObject("update_ip")) {
            entity.setUpdateIp(rs.getString("update_ip"));
        }
        if (fieldSet.contains("update_user") && null != rs.getObject("update_user")) {
            entity.setUpdateUser(rs.getString("update_user"));
        }
        if (fieldSet.contains("name") && null != rs.getObject("name")) {
            entity.setName(rs.getString("name"));
        }
        if (fieldSet.contains("pid") && null != rs.getObject("pid")) {
            entity.setPid(rs.getLong("pid"));
        }
        if (fieldSet.contains("path") && null != rs.getObject("path")) {
            entity.setPath(rs.getString("path"));
        }
        if (fieldSet.contains("gid") && null != rs.getObject("gid")) {
            entity.setGid(rs.getLong("gid"));
        }
        if (fieldSet.contains("site_type") && null != rs.getObject("site_type")) {
            entity.setSiteType(rs.getLong("site_type"));
        }
        if (fieldSet.contains("domain") && null != rs.getObject("domain")) {
            entity.setDomain(rs.getString("domain"));
        }
        if (fieldSet.contains("weixin_appid") && null != rs.getObject("weixin_appid")) {
            entity.setWeixinAppid(rs.getString("weixin_appid"));
        }
        if (fieldSet.contains("weixin_secret") && null != rs.getObject("weixin_secret")) {
            entity.setWeixinSecret(rs.getString("weixin_secret"));
        }
        if (fieldSet.contains("weixin_accounts") && null != rs.getObject("weixin_accounts")) {
            entity.setWeixinAccounts(rs.getString("weixin_accounts"));
        }
        if (fieldSet.contains("weixin_token") && null != rs.getObject("weixin_token")) {
            entity.setWeixinToken(rs.getString("weixin_token"));
        }
        if (fieldSet.contains("sms_config_id") && null != rs.getObject("sms_config_id")) {
            entity.setSmsConfigId(rs.getLong("sms_config_id"));
        }
        if (fieldSet.contains("website_config_id") && null != rs.getObject("website_config_id")) {
            entity.setWebsiteConfigId(rs.getLong("website_config_id"));
        }
        if (fieldSet.contains("no") && null != rs.getObject("no")) {
            entity.setNo(rs.getInt("no"));
        }
        if (fieldSet.contains("customer_service_telephone") && null != rs.getObject("customer_service_telephone")) {
            entity.setCustomerServiceTelephone(rs.getString("customer_service_telephone"));
        }
        return entity;
    }
}